﻿using APP.HELPER.Data.Helper;
using APP.HELPER.Utils.CustomizeToolbox;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using Extra.ERP;
using Security.Login;
using Security.Security;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace Security.ERP
{
    public static class MyRule
    {
        private static bool m_Active;
        private static bool m_AllowAccess;
        private static bool m_AllowAdd;
        private static bool m_AllowDelete;
        private static bool m_AllowEdit;
        private static bool m_AllowExport;
        private static bool m_AllowImport;
        private static bool m_AllowPrint;
        private static string m_Goup_ID;
        private static string m_Object_ID;
        private static string m_Rule_ID;

        public static void AddCombo(System.Windows.Forms.ComboBox combo)
        {
            GridCalendarControlLib.TableToComboBox(combo, GetList(), "SYS_USER_RULEName", "SYS_USER_RULEID");
        }

        public static void AddComboAll(System.Windows.Forms.ComboBox combo)
        {
            DataTable list = new DataTable();
            list = GetList();
            DataRow row = list.NewRow();
            row["SYS_USER_RULEID"] = "All";
            row["SYS_USER_RULEName"] = "T?t c?";
            list.Rows.InsertAt(row, 0);
            GridCalendarControlLib.TableToComboBox(combo, list, "SYS_USER_RULEName", "SYS_USER_RULEID");
        }

        public static bool Check(string myObject)
        {
            return Check(EnumRule.All, myObject);
        }

        public static bool Check(EnumRule rule, string myObject)
        {
            if (Get(MyLogin.RoleId, myObject) != "OK")
            {
                return false;
            }
            switch (rule)
            {
                case EnumRule.AllowAccess:
                    return AllowAccess;

                case EnumRule.AllowAdd:
                    return AllowAdd;

                case EnumRule.AllowDelete:
                    return AllowDelete;

                case EnumRule.AllowEdit:
                    return AllowEdit;

                case EnumRule.AllowPrint:
                    return AllowPrint;

                case EnumRule.AllowExport:
                    return AllowExport;

                case EnumRule.AllowImport:
                    return AllowImport;
            }
            return true;
        }

        public static string CreateKey()
        {
            return CreateKey("");
        }

        public static string CreateKey(SqlTransaction myTransaction)
        {
            return SqlHelper.GenCode(myTransaction, "");
        }

        public static string CreateKey(string key)
        {
            return SqlHelper.GenCode(key);
        }

        public static string CreateKey(SqlTransaction myTransaction, string key)
        {
            return SqlHelper.GenCode(myTransaction, key);
        }

        public static string Delete()
        {
            return Delete(m_Goup_ID, m_Object_ID);
        }

        public static string Delete(SqlConnection myConnection)
        {
            return Delete(myConnection, m_Goup_ID, m_Object_ID);
        }

        public static string Delete(SqlTransaction myTransaction)
        {
            return Delete(m_Goup_ID, m_Object_ID);
        }

        public static string Delete(string Goup_ID, string Object_ID)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID" };
            object[] myValues = new object[] { Goup_ID, Object_ID };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteNonQuery("SYS_USER_RULE_Delete", myParams, myValues);
        }

        public static string Delete(SqlConnection myConnection, string Goup_ID, string Object_ID)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID" };
            object[] myValues = new object[] { Goup_ID, Object_ID };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "SYS_USER_RULE_Delete", myParams, myValues);
        }

        public static string Delete(SqlTransaction myTransaction, string Goup_ID, string Object_ID)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID" };
            object[] myValues = new object[] { Goup_ID, Object_ID };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "SYS_USER_RULE_Delete", myParams, myValues);
        }

        public static bool Exist()
        {
            return Exist(m_Goup_ID, m_Object_ID);
        }

        public static bool Exist(SqlTransaction myTransaction)
        {
            return Exist(myTransaction, m_Goup_ID, m_Object_ID);
        }

        public static bool Exist(string Goup_ID, string Object_ID)
        {
            bool hasRows = false;
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID" };
            object[] myValues = new object[] { Goup_ID, Object_ID };
            SqlHelper helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("SYS_USER_RULE_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                helper.Close();
                reader = null;
            }
            return hasRows;
        }

        public static bool Exist(SqlTransaction myTransaction, string Goup_ID, string Object_ID)
        {
            bool hasRows = false;
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID" };
            object[] myValues = new object[] { Goup_ID, Object_ID };
            SqlDataReader reader = new SqlHelper().ExecuteReader(myTransaction, "SYS_USER_RULE_Get", myParams, myValues);
            if (reader != null)
            {
                hasRows = reader.HasRows;
                reader.Close();
                reader = null;
            }
            return hasRows;
        }

        public static string Get()
        {
            return Get(Goup_ID, Object_ID);
        }

        public static string Get(SqlConnection myConnection)
        {
            return Get(myConnection, Goup_ID, Object_ID);
        }

        public static string Get(SqlTransaction myTransaction)
        {
            return Get(myTransaction, Goup_ID, Object_ID);
        }

        public static string Get(string Goup_ID, string Object_ID)
        {
            string str = "";
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID" };
            object[] myValues = new object[] { Goup_ID, Object_ID };
            SqlHelper helper = new SqlHelper();
            SqlDataReader reader = helper.ExecuteReader("SYS_USER_RULE_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_Goup_ID = Convert.ToString(reader["Goup_ID"]);
                    m_Object_ID = Convert.ToString(reader["Object_ID"]);
                    m_Rule_ID = Convert.ToString(reader["Rule_ID"]);
                    m_AllowAdd = Convert.ToBoolean(reader["AllowAdd"]);
                    m_AllowDelete = Convert.ToBoolean(reader["AllowDelete"]);
                    m_AllowEdit = Convert.ToBoolean(reader["AllowEdit"]);
                    m_AllowAccess = Convert.ToBoolean(reader["AllowAccess"]);
                    m_AllowPrint = Convert.ToBoolean(reader["AllowPrint"]);
                    m_AllowExport = Convert.ToBoolean(reader["AllowExport"]);
                    m_AllowImport = Convert.ToBoolean(reader["AllowImport"]);
                    m_Active = Convert.ToBoolean(reader["Active"]);
                    str = "OK";
                }
                reader.Close();
                reader = null;
            }
            helper.Close();
            return str;
        }

        public static string Get(SqlConnection myConnection, string Goup_ID, string Object_ID)
        {
            string str = "";
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID" };
            object[] myValues = new object[] { Goup_ID, Object_ID };
            SqlDataReader reader = new SqlHelper().ExecuteReader(myConnection, "SYS_USER_RULE_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_Goup_ID = Convert.ToString(reader["Goup_ID"]);
                    m_Object_ID = Convert.ToString(reader["Object_ID"]);
                    m_Rule_ID = Convert.ToString(reader["Rule_ID"]);
                    m_AllowAdd = Convert.ToBoolean(reader["AllowAdd"]);
                    m_AllowDelete = Convert.ToBoolean(reader["AllowDelete"]);
                    m_AllowEdit = Convert.ToBoolean(reader["AllowEdit"]);
                    m_AllowAccess = Convert.ToBoolean(reader["AllowAccess"]);
                    m_AllowPrint = Convert.ToBoolean(reader["AllowPrint"]);
                    m_AllowExport = Convert.ToBoolean(reader["AllowExport"]);
                    m_AllowImport = Convert.ToBoolean(reader["AllowImport"]);
                    m_Active = Convert.ToBoolean(reader["Active"]);
                    str = "OK";
                }
                reader.Close();
                reader = null;
            }
            return str;
        }

        public static string Get(SqlTransaction myTransaction, string Goup_ID, string Object_ID)
        {
            string str = "";
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID" };
            object[] myValues = new object[] { Goup_ID, Object_ID };
            SqlDataReader reader = new SqlHelper().ExecuteReader(myTransaction, "SYS_USER_RULE_Get", myParams, myValues);
            if (reader != null)
            {
                while (reader.Read())
                {
                    m_Goup_ID = Convert.ToString(reader["Goup_ID"]);
                    m_Object_ID = Convert.ToString(reader["Object_ID"]);
                    m_Rule_ID = Convert.ToString(reader["Rule_ID"]);
                    m_AllowAdd = Convert.ToBoolean(reader["AllowAdd"]);
                    m_AllowDelete = Convert.ToBoolean(reader["AllowDelete"]);
                    m_AllowEdit = Convert.ToBoolean(reader["AllowEdit"]);
                    m_AllowAccess = Convert.ToBoolean(reader["AllowAccess"]);
                    m_AllowPrint = Convert.ToBoolean(reader["AllowPrint"]);
                    m_AllowExport = Convert.ToBoolean(reader["AllowExport"]);
                    m_AllowImport = Convert.ToBoolean(reader["AllowImport"]);
                    m_Active = Convert.ToBoolean(reader["Active"]);
                    str = "OK";
                }
                reader.Close();
                reader = null;
            }
            return str;
        }

        public static DataTable GetList()
        {
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteDataTable("SYS_USER_RULE_GetList");
        }

        public static DataTable GetList(string Goup_ID, string Language_Id, string Owner)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Language_Id", "@Owner" };
            object[] myValues = new object[] { Goup_ID, Language_Id, Owner };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteDataTable("Permision", myParams, myValues);
        }

        public static string Insert()
        {
            return Insert(m_Goup_ID, m_Object_ID, m_Rule_ID, m_AllowAdd, m_AllowDelete, m_AllowEdit, m_AllowAccess, m_AllowPrint, m_AllowExport, m_AllowImport, m_Active);
        }

        public static string Insert(SqlConnection myConnection)
        {
            return Insert(myConnection, m_Goup_ID, m_Object_ID, m_Rule_ID, m_AllowAdd, m_AllowDelete, m_AllowEdit, m_AllowAccess, m_AllowPrint, m_AllowExport, m_AllowImport, m_Active);
        }

        public static string Insert(SqlTransaction myTransaction)
        {
            return Insert(myTransaction, m_Goup_ID, m_Object_ID, m_Rule_ID, m_AllowAdd, m_AllowDelete, m_AllowEdit, m_AllowAccess, m_AllowPrint, m_AllowExport, m_AllowImport, m_Active);
        }

        public static string Insert(string Goup_ID, string Object_ID, string Rule_ID, bool AllowAdd, bool AllowDelete, bool AllowEdit, bool AllowAccess, bool AllowPrint, bool AllowExport, bool AllowImport, bool Active)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID", "@Rule_ID", "@AllowAdd", "@AllowDelete", "@AllowEdit", "@AllowAccess", "@AllowPrint", "@AllowExport", "@AllowImport", "@Active" };
            object[] myValues = new object[] { Goup_ID, Object_ID, Rule_ID, AllowAdd, AllowDelete, AllowEdit, AllowAccess, AllowPrint, AllowExport, AllowImport, Active };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteNonQuery("SYS_USER_RULE_Insert", myParams, myValues);
        }

        public static string Insert(SqlConnection myConnection, string Goup_ID, string Object_ID, string Rule_ID, bool AllowAdd, bool AllowDelete, bool AllowEdit, bool AllowAccess, bool AllowPrint, bool AllowExport, bool AllowImport, bool Active)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID", "@Rule_ID", "@AllowAdd", "@AllowDelete", "@AllowEdit", "@AllowAccess", "@AllowPrint", "@AllowExport", "@AllowImport", "@Active" };
            object[] myValues = new object[] { Goup_ID, Object_ID, Rule_ID, AllowAdd, AllowDelete, AllowEdit, AllowAccess, AllowPrint, AllowExport, AllowImport, Active };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "SYS_USER_RULE_Insert", myParams, myValues);
        }

        public static string Insert(SqlTransaction myTransaction, string Goup_ID, string Object_ID, string Rule_ID, bool AllowAdd, bool AllowDelete, bool AllowEdit, bool AllowAccess, bool AllowPrint, bool AllowExport, bool AllowImport, bool Active)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID", "@Rule_ID", "@AllowAdd", "@AllowDelete", "@AllowEdit", "@AllowAccess", "@AllowPrint", "@AllowExport", "@AllowImport", "@Active" };
            object[] myValues = new object[] { Goup_ID, Object_ID, Rule_ID, AllowAdd, AllowDelete, AllowEdit, AllowAccess, AllowPrint, AllowExport, AllowImport, Active };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "SYS_USER_RULE_Insert", myParams, myValues);
        }

        public static bool IsAccess(string myObject)
        {
            return Check(EnumRule.AllowAccess, myObject);
        }

        public static bool IsAdd(string myObject)
        {
            return Check(EnumRule.AllowAdd, myObject);
        }

        public static bool IsAdmin()
        {
            new xfmIsAdmin().ShowDialog();
            return xfmIsAdmin.isAdmin;
        }

        public static bool IsDelete(string myObject)
        {
            return Check(EnumRule.AllowDelete, myObject);
        }

        public static bool IsEdit(string myObject)
        {
            return Check(EnumRule.AllowEdit, myObject);
        }

        public static bool IsExport(string myObject)
        {
            return Check(EnumRule.AllowExport, myObject);
        }

        public static bool IsImport(string myObject)
        {
            return Check(EnumRule.AllowImport, myObject);
        }

        public static bool IsPrint(string myObject)
        {
            return Check(EnumRule.AllowPrint, myObject);
        }

        public static string NewID()
        {
            return SqlHelper.GenCode("SYS_USER_RULE", "SYS_USER_RULEID", "");
        }

        public static void Notify()
        {
            XtraMessageBox.Show(MsgResc.Permision, "Cảnh Báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        public static void Notify(IWin32Window sender)
        {
            XtraMessageBox.Show(sender, MsgResc.Permision, "C?nh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }

        public static string Update()
        {
            return Update(m_Goup_ID, m_Object_ID, m_Rule_ID, m_AllowAdd, m_AllowDelete, m_AllowEdit, m_AllowAccess, m_AllowPrint, m_AllowExport, m_AllowImport, m_Active);
        }

        public static string Update(SqlConnection myConnection)
        {
            return Update(myConnection, m_Goup_ID, m_Object_ID, m_Rule_ID, m_AllowAdd, m_AllowDelete, m_AllowEdit, m_AllowAccess, m_AllowPrint, m_AllowExport, m_AllowImport, m_Active);
        }

        public static string Update(SqlTransaction myTransaction)
        {
            return Update(myTransaction, m_Goup_ID, m_Object_ID, m_Rule_ID, m_AllowAdd, m_AllowDelete, m_AllowEdit, m_AllowAccess, m_AllowPrint, m_AllowExport, m_AllowImport, m_Active);
        }

        public static string Update(string Goup_ID, string Object_ID, string Rule_ID, bool AllowAdd, bool AllowDelete, bool AllowEdit, bool AllowAccess, bool AllowPrint, bool AllowExport, bool AllowImport, bool Active)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID", "@Rule_ID", "@AllowAdd", "@AllowDelete", "@AllowEdit", "@AllowAccess", "@AllowPrint", "@AllowExport", "@AllowImport", "@Active" };
            object[] myValues = new object[] { Goup_ID, Object_ID, Rule_ID, AllowAdd, AllowDelete, AllowEdit, AllowAccess, AllowPrint, AllowExport, AllowImport, Active };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteNonQuery("SYS_USER_RULE_Update", myParams, myValues);
        }

        public static string Update(SqlConnection myConnection, string Goup_ID, string Object_ID, string Rule_ID, bool AllowAdd, bool AllowDelete, bool AllowEdit, bool AllowAccess, bool AllowPrint, bool AllowExport, bool AllowImport, bool Active)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID", "@Rule_ID", "@AllowAdd", "@AllowDelete", "@AllowEdit", "@AllowAccess", "@AllowPrint", "@AllowExport", "@AllowImport", "@Active" };
            object[] myValues = new object[] { Goup_ID, Object_ID, Rule_ID, AllowAdd, AllowDelete, AllowEdit, AllowAccess, AllowPrint, AllowExport, AllowImport, Active };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteNonQuery(myConnection, "SYS_USER_RULE_Update", myParams, myValues);
        }

        public static string Update(SqlTransaction myTransaction, string Goup_ID, string Object_ID, string Rule_ID, bool AllowAdd, bool AllowDelete, bool AllowEdit, bool AllowAccess, bool AllowPrint, bool AllowExport, bool AllowImport, bool Active)
        {
            string[] myParams = new string[] { "@Goup_ID", "@Object_ID", "@Rule_ID", "@AllowAdd", "@AllowDelete", "@AllowEdit", "@AllowAccess", "@AllowPrint", "@AllowExport", "@AllowImport", "@Active" };
            object[] myValues = new object[] { Goup_ID, Object_ID, Rule_ID, AllowAdd, AllowDelete, AllowEdit, AllowAccess, AllowPrint, AllowExport, AllowImport, Active };
            SqlHelper helper = new SqlHelper();
            return helper.ExecuteNonQuery(myTransaction, "SYS_USER_RULE_Update", myParams, myValues);
        }

        public static BarItemVisibility Accessed
        {
            get
            {
                if (m_AllowAccess)
                {
                    return BarItemVisibility.Always;
                }
                return BarItemVisibility.Never;
            }
        }

        public static bool Active
        {
            get
            {
                return m_Active;
            }
            set
            {
                m_Active = value;
            }
        }

        public static BarItemVisibility Added
        {
            get
            {
                if (m_AllowAdd)
                {
                    return BarItemVisibility.Always;
                }
                return BarItemVisibility.Never;
            }
        }

        public static bool AllowAccess
        {
            get
            {
                return m_AllowAccess;
            }
            set
            {
                m_AllowAccess = value;
            }
        }

        public static bool AllowAdd
        {
            get
            {
                return m_AllowAdd;
            }
            set
            {
                m_AllowAdd = value;
            }
        }

        public static bool AllowDelete
        {
            get
            {
                return m_AllowDelete;
            }
            set
            {
                m_AllowDelete = value;
            }
        }

        public static bool AllowEdit
        {
            get
            {
                return m_AllowEdit;
            }
            set
            {
                m_AllowEdit = value;
            }
        }

        public static bool AllowExport
        {
            get
            {
                return m_AllowExport;
            }
            set
            {
                m_AllowExport = value;
            }
        }

        public static bool AllowImport
        {
            get
            {
                return m_AllowImport;
            }
            set
            {
                m_AllowImport = value;
            }
        }

        public static bool AllowPrint
        {
            get
            {
                return m_AllowPrint;
            }
            set
            {
                m_AllowPrint = value;
            }
        }

        public static string Copyright
        {
            get
            {
                return "C\x00f4ng Ty Ph?n M?m Ho\x00e0n H?o";
            }
        }

        public static BarItemVisibility Deleted
        {
            get
            {
                if (m_AllowDelete)
                {
                    return BarItemVisibility.Always;
                }
                return BarItemVisibility.Never;
            }
        }

        public static BarItemVisibility Edited
        {
            get
            {
                if (m_AllowEdit)
                {
                    return BarItemVisibility.Always;
                }
                return BarItemVisibility.Never;
            }
        }

        public static BarItemVisibility Exported
        {
            get
            {
                if (m_AllowExport)
                {
                    return BarItemVisibility.Always;
                }
                return BarItemVisibility.Never;
            }
        }

        public static string Goup_ID
        {
            get
            {
                return m_Goup_ID;
            }
            set
            {
                m_Goup_ID = value;
            }
        }

        public static BarItemVisibility Imported
        {
            get
            {
                if (m_AllowImport)
                {
                    return BarItemVisibility.Always;
                }
                return BarItemVisibility.Never;
            }
        }

        public static string Object_ID
        {
            get
            {
                return m_Object_ID;
            }
            set
            {
                m_Object_ID = value;
            }
        }

        public static BarItemVisibility Printed
        {
            get
            {
                if (m_AllowPrint)
                {
                    return BarItemVisibility.Always;
                }
                return BarItemVisibility.Never;
            }
        }

        public static string ProductName
        {
            get
            {
                return "Class SYS_USER_RULE";
            }
        }

        public static string ProductVersion
        {
            get
            {
                return "1.0.0.0";
            }
        }

        public static string Rule_ID
        {
            get
            {
                return m_Rule_ID;
            }
            set
            {
                m_Rule_ID = value;
            }
        }

        public enum EnumRule
        {
            All,
            AllowAccess,
            AllowAdd,
            AllowDelete,
            AllowEdit,
            AllowPrint,
            AllowExport,
            AllowImport
        }
    }
}